package com.changgou.config.startup;

import com.changgou.config.pojo.DictData;
import com.changgou.config.pojo.DictType;
import com.changgou.config.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jiang Yilin
 * @title: startup
 * @projectName changgou_parent
 * @description: TODO
 * @date 2020/3/618:06
 */
@Component
public class InitSysDitcData implements CommandLineRunner {

    @Autowired
    ISysDictService iSysDictService;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {

        //1. 加载type
        List<DictType> dictTypeList = iSysDictService.findAll();

        if(dictTypeList!=null && dictTypeList.size()>0){
            dictTypeList.forEach(
                    type->{
                        List<DictData> datas = iSysDictService.findByType(type.getDictType());
                        if(datas!=null && datas.size()>0){
                            //把datas放入缓存
                            datas.forEach(data->{
                             redisTemplate.opsForHash().put(data.getDictType(),data.getDictValue(),data.getDictLabel());
                            });
                        }
                    }
            );
        }


    }
}
